package org.jeecg.modules.demo.crm.mapper;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.demo.crm.entity.CPaymentInvoice;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * @Description: 收票登记
 * @Author: jeecg-boot
 * @Date:   2025-06-14
 * @Version: V1.0
 */
public interface CPaymentInvoiceMapper extends BaseMapper<CPaymentInvoice> {

    @Select("select IFNULL(sum(amount),0) as totla from c_payment_invoice  where project_id=#{projectId}  and del_flag = 0 ")
    BigDecimal getTotalAmount(@Param("projectId") String projectId);


    @Select("<script> select IFNULL(sum(amount),0) as totla from c_payment_invoice  where project_id=#{projectId}  and del_flag = 0 <if test='subProjectIds != null and subProjectIds.size() > 0'>  AND sub_project_id IN <foreach collection='subProjectIds' item='id' open='(' separator=',' close=')'>#{id}</foreach> </if> </script>")
    BigDecimal getTotalAmountBySubProjectId(@Param("projectId") String projectId ,@Param("subProjectIds") List<String> subProjectIds);

    @Select("select IFNULL(sum(tax_amount),0) as totla from c_payment_invoice  where project_id=#{projectId}  and del_flag = 0 ")
    BigDecimal getReceiptsTaxTotal(@Param("projectId") String projectId);

    /**
     * 收票合计金额
     * @param projectId
     * @param subProjectIds
     * @return
     */
    @Select("<script> select IFNULL(sum(total_amount_with_tax),0) as totla from c_payment_invoice  where project_id=#{projectId}  and del_flag = 0 <if test='subProjectIds != null and subProjectIds.size() > 0'>  AND sub_project_id IN <foreach collection='subProjectIds' item='id' open='(' separator=',' close=')'>#{id}</foreach> </if> </script>")
    BigDecimal getTotalAmountWithTaxBySubProjectId(@Param("projectId") String projectId,@Param("subProjectIds") List<String> subProjectIds);

    @Select("select * from c_payment_invoice where del_flag = 0")
    List<CPaymentInvoice> findiInvoiceSplit();

    @Select("select * from c_payment_invoice where del_flag = 0 and invoice_number=#{invoiceNumber}")
    CPaymentInvoice getInvoiceNumber(@Param("invoiceNumber")String invoiceNumber);

    /**
     * 应付合计 收票--并且未关联付款的
     * @param projectId
     * @return
     */
    @Select("select  IFNULL(sum(t1.total_amount_with_tax),0) from c_payment_invoice as t1 LEFT JOIN c_payment_request as t2 on t1.id = t2.invoice_id where t1.del_flag = 0 AND t2.id IS NULL and t1.project_id=#{projectId}")
    BigDecimal totalPayable(@Param("projectId") String projectId);


    @Select("select IFNULL(sum(total_amount_with_tax),0) as totla from c_payment_invoice  where project_id=#{projectId} and  sub_project_id = #{subProjectIds} ")
    BigDecimal getTotalAmountWithTaxSub(@Param("projectId") String projectId,@Param("subProjectIds") String subProjectIds);
}
